<!DOCTYPE html>
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="/static/admin/component/pear/css/pear.css" />
</head>
<body>
<form class="layui-form" id="myForm" method="post" action="{:url('/admin/news/addNews')}" enctype="multipart/form-data">
    <div class="mainBox">
        <div class="main-container">
            <div class="main-container">
                <div class="layui-form-item">
                    <label class="layui-form-label">作者</label>
                    <div class="layui-input-block">
                        <input type="text" name="author" id="author" lay-verify="author" autocomplete="off" placeholder="请输入作者名称" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">标题</label>
                    <div class="layui-input-block">
                        <input type="text" name="title" id="title" lay-verify="title" autocomplete="off" placeholder="请输入标题" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item upload">
                    <label class="layui-form-label">头像</label>
                    <button type="button" name="upload" class="layui-btn layui-btn-primary layui-upload" lay-type="image"
                            lay-data="{ exts:'jpg|png|gif|jpeg|ico', accept:'image'}">请上传图片</button>
                    <input type="hidden" class="upload-input field-avatar" name="avatar" id="avatar" value="">
                    <img src="" style="display:none;border-radius:5px;border:1px solid #ccc" width="70" height="40" id="show1">
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">简介</label>
                    <div class="layui-input-block">
                        <input type="text" name="content" id="content" lay-verify="content" autocomplete="off" placeholder="请输入文章简介" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">文章内容</label>
                    <div class="layui-input-inline w600">
                        <textarea id="detail"  class="layui-textarea field-detail" name="detail" lay-verify="detail" autocomplete="off"></textarea>
                    </div>
                </div>
                <div class="layui-form-item upload">
                    <label class="layui-form-label">文章外图片</label>
                    <button type="button" name="upload" class="layui-btn layui-btn-primary layui-upload2" lay-type="image"
                            lay-data="{ exts:'jpg|png|gif|jpeg|ico', accept:'image'}">请上传图片</button>
                    <input type="hidden" class="upload-input field-imgSrc" id="imgSrc" name="imgSrc" value="">
                    <img src="" style="display:none;border-radius:5px;border:1px solid #ccc" width="100" height="60" id="show2">
                </div>
                <div class="layui-form-item upload">
                    <label class="layui-form-label">文章内图片</label>
                    <button type="button" name="upload" class="layui-btn layui-btn-primary layui-upload3" lay-type="image"
                            lay-data="{ exts:'jpg|png|gif|jpeg|ico', accept:'image'}">请上传图片</button>
                    <input type="hidden" class="upload-input field-headImgSrc" id="headImgSrc" name="headImgSrc" value="">
                    <img src="" style="display:none;border-radius:5px;border:1px solid #ccc" width="100" height="60" id="show3">
                </div>
            </div>
            <div class="button-container">
                <button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit="" lay-filter="add">提交</button>
                <button type="reset" class="pear-btn pear-btn-sm" onclick="location='cms.html'">返回</button>
            </div>
        </div>
    </div>
</form>
<script src="/static/admin/component/layui/layui.js"></script>
<script src="/static/admin/component/pear/pear.js"></script>
<script>
    layui.use(['tinymce'], function() {
        var tinymce = layui.tinymce;
        tinymce.render({
            elem: "#detail",
            width:800,
            height: 700
        });
    });
    layui.use(['form','jquery','upload'],function(){
        let form = layui.form;
        let $ = layui.jquery;
        let upload = layui.upload;

        form.on('submit(user-save)', function(data){
            let roleIds = "";
            $('input[type=checkbox]:checked').each(function() {
                roleIds += $(this).val()+",";
            });
            roleIds = roleIds.substr(0,roleIds.length-1);
            data.field.roleIds = roleIds;
            $.ajax({
                url:'/system/user/save',
                data:JSON.stringify(data.field),
                dataType:'json',
                contentType:'application/json',
                type:'post',
                success:function(result){
                    if(result.success){
                        layer.msg(result.msg,{icon:1,time:1000},function(){
                            parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
                            parent.layui.table.reload("user-table");
                        });
                    }else{
                        layer.msg(result.msg,{icon:2,time:1000});
                    }
                }
            })
            return false;
        });

        upload.render({
            elem: '.layui-upload',
            url: "{:url('/admin/banner/upload')}"
            ,method: 'post'
            ,before: function(input) {
                layer.msg('文件上传中...', {time:3000000});
            },done: function(res) {
                var obj = this.item;
                if (res.code == 0) {
                    layer.msg(res.msg);
                    $("#show1").attr('src',res.file).show();
                    $('#avatar').val(res.file);
                    return false;
                }
                layer.closeAll();
            }
        });
        upload.render({
            elem: '.layui-upload2',
            url: "{:url('/admin/banner/upload')}"
            ,method: 'post'
            ,before: function(input) {
                layer.msg('文件上传中...', {time:3000000});
            },done: function(res) {
                var obj = this.item;
                if (res.code == 0) {
                    layer.msg(res.msg);
                    $("#show2").attr('src',res.file).show();
                    $('#imgSrc').val(res.file);
                    return false;
                }
                layer.closeAll();
            }
        });
        upload.render({
            elem: '.layui-upload3',
            url: "{:url('/admin/banner/upload')}"
            ,method: 'post'
            ,before: function(input) {
                layer.msg('文件上传中...', {time:3000000});
            },done: function(res) {
                var obj = this.item;
                if (res.code == 0) {
                    layer.msg(res.msg);
                    $("#show3").attr('src',res.file).show();
                    $('#headImgSrc').val(res.file);
                    return false;
                }
                layer.closeAll();
            }
        });
    })
</script>
</body>
</html>